﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using CommunitySystem.BO;
using CommunitySystemModel;

namespace CommunitySystem.Web.Service
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“JinShengService”。
    public class JinShengService : IJinShengService
    {
        private readonly JinShengBO bo = new JinShengBO();


        public ObservableCollection<JinShen_MainInfo> GetJinShen_MainInfo(string condition, string LoginUser)
        {
            return bo.GetJinshenMainInfo(condition, LoginUser);
        }

        public void SaveJinShenMainInfo(JinShen_MainInfo entity)
        {   
            //if (entity.Status == "晋升确认" && entity.ID > 0)//确认晋升  完成一系列动作
            //{
            //    // 简历信息加一条
            //    // 职务加一条  在任  前面的改为不在任
            //    // Chegai 信息增加一条  
            //    try
            //    {

            //    }

            //}
            //else
            //{
                bo.SaveJinShen_MainInfor(entity);
           // }
        }

        public void DeleteJinShenMainInfo(JinShen_MainInfo entity)
        {
            AccessHelper.GetInstance().DeleteEntity("JinShen_MainInfo", entity.ID);
            foreach (var d in entity.Details)
            {
                AccessHelper.GetInstance().DeleteEntity("JinShen_DetailInfo", entity.ID);
            }
        }

        public void SaveJinShenDetail(JinShen_DetailInfo entity)
        {
            AccessHelper.GetInstance().SaveEntity(entity, "", null);
        }


        public ObservableCollection<Emp_BasicInfoModel> GetEmpBasicInfo(string condition)
        {
            return bo.GetEmpBasicInfo(condition);
        }


        public ObservableCollection<Emp_BasicInfoModel> GetEmpBasicInfoAccordingToDuty(string condition)
        {
            return bo.GetEmpBasicInfoAccordingToDuty(condition);
        }


        public ObservableCollection<JinShen_MainInfo_Check> GetMainInfo_Check(string condition)
        {
            return AccessHelper.GetInstance().GetEnity<JinShen_MainInfo_Check>(condition, "");
        }

        public bool SaveMainInfo_Check(JinShen_MainInfo_Check entity)
        {
            try
            {
                AccessHelper.GetInstance().SaveEntity(entity, "", null);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public ObservableCollection<JinShen_CompetSpeech_Main> GetCompetSpeechMain(string condition)
        {
            var mains = AccessHelper.GetInstance().GetEnity<JinShen_CompetSpeech_Main>(condition, "");
            foreach (var m in mains)
            {
                m.Details = AccessHelper.GetInstance().GetEnity<JinShen_CompetSpeech_Detail>("SpeechMainID=" + m.ID, "");
            }
            return mains;
        }

        public bool SaveJinShen_CompetSpeech_Main(JinShen_CompetSpeech_Main entity)
        {
            try
            {
                if (entity.ID > 0)
                {
                    string sql = "delete JinShen_CompetSpeech_Detail where SpeechMainID=" + entity.ID;
                    AccessHelper.GetInstance().ExecuteNonQuery(sql);
                    int id = AccessHelper.GetInstance().SaveEntity(entity, "", new List<string>() { "Details" });
                    foreach (var detail in entity.Details)
                    {
                        detail.SpeechMainID = id;
                        detail.ID = 0;
                        AccessHelper.GetInstance().SaveEntity(detail, "", null);
                    }
                }
                else
                {
                    int id = AccessHelper.GetInstance().SaveEntity(entity, "", new List<string>() { "Details" });
                    foreach (var detail in entity.Details)
                    {
                        detail.SpeechMainID = id;
                        AccessHelper.GetInstance().SaveEntity(detail, "", null);
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public ObservableCollection<JinShen_Democraticevaluation_Main> GetDemocraEvalution(string condition)
        {
            var mains = AccessHelper.GetInstance().GetEnity<JinShen_Democraticevaluation_Main>(condition, "");
            foreach (var m in mains)
            {
                m.Details = AccessHelper.GetInstance().GetEnity<JinShen_Democraticevaluation_Detail>("DemoEvaMainID=" + m.ID, "");
            }
            return mains;
        }

        public bool SaveJinShen_Democraticevaluation_Main(JinShen_Democraticevaluation_Main entity)
        {
            try
            {
                if (entity.ID > 0)
                {
                    string sql = "delete JinShen_Democraticevaluation_Detail where DemoEvaMainID=" + entity.ID;
                    AccessHelper.GetInstance().ExecuteNonQuery(sql);
                    int id = AccessHelper.GetInstance().SaveEntity(entity, "", new List<string>() { "Details" });
                    foreach (var detail in entity.Details)
                    {
                        detail.DemoEvaMainID = id;
                        detail.ID = 0;
                        AccessHelper.GetInstance().SaveEntity(detail, "", null);
                    }
                }
                else
                {
                    int id = AccessHelper.GetInstance().SaveEntity(entity, "", new List<string>() { "Details" });
                    foreach (var detail in entity.Details)
                    {
                        detail.DemoEvaMainID = id;
                        AccessHelper.GetInstance().SaveEntity(detail, "", null);
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool SaveDetailCheck(JinShen_Democraticevaluation_Main evaMain, JinShen_CompetSpeech_Main speechMain, JinShen_MainInfo main)
        {
            try
            {
                bo.SaveJinShen_MainInfor(main);
                this.SaveJinShen_Democraticevaluation_Main(evaMain);
                if (speechMain != null)
                {
                    this.SaveJinShen_CompetSpeech_Main(speechMain);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        public string GetOrgForHR(string loginUser)
        {
            return bo.GetOrgForHR(loginUser);
        }


        public bool SaveJinShen_ChooseCandidator_Main(JinShen_ChooseCandidator_Main entity)
        {
            return bo.SaveJinShen_ChooseCandidator_Main(entity);
        }


        public ObservableCollection<JinShen_ChooseCandidator_Main> GetJinShen_ChooseCandidator_Main(string condition)
        {
            ObservableCollection<JinShen_ChooseCandidator_Main> mains = null;
            mains = AccessHelper.GetInstance().GetEnity<JinShen_ChooseCandidator_Main>(condition, "");
            foreach (var main in mains)
            {
                main.Details = AccessHelper.GetInstance().GetEnity<JinShen_ChooseCandidator_Detail>("ChooseMainID=" + main.ID, "");
            }
            return mains;
        }


        public void SaveJinShen_KaoCha_Detail(JinShen_KaoCha_Detail[] details)
        {
            if (details != null && details.Length > 0)
            {
                string sql = "delete JinShen_KaoCha_Detail where MainID = " + details.FirstOrDefault().MainID;
                AccessHelper.GetInstance().ExecuteNonQuery(sql);
            }
            foreach (var d in details)
            {
                AccessHelper.GetInstance().SaveEntity(d, "", null);
            }
        }

        public ObservableCollection<JinShen_KaoCha_Detail> GetJinShen_KaoCha_Detail(string condition)
        {
            return AccessHelper.GetInstance().GetEnity<JinShen_KaoCha_Detail>(condition, "");
        }


        public void SaveJinShen_DangGongWei_Detail(JinShen_DangGongWei_Detail[] details)
        {
            if (details != null && details.Length > 0)
            {
                string sql = "delete JinShen_DangGongWei_Detail where MainID = " + details.FirstOrDefault().MainID;
                AccessHelper.GetInstance().ExecuteNonQuery(sql);
            }
            foreach (var d in details)
            {
                d.ID = 0;
                AccessHelper.GetInstance().SaveEntity(d, "", null);
            }
        }

        public ObservableCollection<JinShen_DangGongWei_Detail> GetJinShen_DangGongWei_Detail(string condition)
        {
            return AccessHelper.GetInstance().GetEnity<JinShen_DangGongWei_Detail>(condition, "");
        }

        public void SaveJinShen_PublicShow_Detail(JinShen_PublicShow_Detail[] details)
        {
            if (details != null && details.Length > 0)
            {
                string sql = "delete JinShen_PublicShow_Detail where MainID = " + details.FirstOrDefault().MainID;
                AccessHelper.GetInstance().ExecuteNonQuery(sql);    
            }
            foreach (var d in details)
            {
                AccessHelper.GetInstance().SaveEntity(d, "", null);
            }
        }

        public ObservableCollection<JinShen_PublicShow_Detail> GetJinShen_PublicShow_Detail(string condition)
        {
            return AccessHelper.GetInstance().GetEnity<JinShen_PublicShow_Detail>(condition, "");
        }





        public string DownLoadFile(string key, string GuidName, string RealName)
        {
            return bo.DownLoadFile(key, GuidName, RealName);
        }


        public bool ComfirmJinShen(int MainInfoID, string Stauts)
        {
            return bo.ComfirmJinShen(MainInfoID, Stauts);
        }
    }
}
